package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.ContactNote;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class ContactNoteDao {
	private DBHelp<ContactNote> db = new DBHelp<ContactNote>();
	
	private class ContactNoteMap implements RowMapper<ContactNote>{

		@Override
		public ContactNote mapperRow(ResultSet rs) throws SQLException {
			ContactNote cn = new ContactNote();
			
			cn.setContactId(rs.getInt("contactid"));
			cn.setContent(rs.getString("content"));
			cn.setCreateTime(rs.getString("createtime"));
			cn.setEmployeeId(rs.getInt("employeeid"));
			cn.setId(rs.getInt("id"));
			cn.setName(rs.getString("name"));
			return cn;
		}
	}
	
	public List<ContactNote> findCNByConId(int id) {
		String sql = "SELECT a.id AS id, contactid, content, createtime, employeeid, b.name AS name FROM t_contactnote AS a, t_employee AS b WHERE a.employeeid = b.id AND contactid = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new ContactNoteMap(), id);
	}

	public List<ContactNote> getNoteByIdAndTime(int contactId, int noteId,
			String lastTime) {
		String sql = "SELECT a.id AS id, contactid, content, createtime, employeeid, b.name AS name FROM t_contactnote AS a, t_employee AS b WHERE a.employeeid = b.id AND contactid = ? AND a.id <> ? AND createtime >= ? ORDER BY createtime";
		return db.executeQueryForList(sql, new ContactNoteMap(), contactId, noteId, lastTime);
	}

	public void insertNote(ContactNote cn) {
		String sql = "INSERT INTO t_contactnote (contactid, content, createtime, employeeid) VALUES (?, ?, ?, ?)";
		db.executeSQL(sql, cn.getContactId(), cn.getContent(), cn.getCreateTime(), cn.getEmployeeId());
	}
}
